import numpy as np
import csv
import random
from datetime import datetime
from time import strftime


def getCurTime():
    """
    get current time
    Return value of the date string format(%Y-%m-%d %H:%M:%S)
    """
    format='%Y-%m-%d %H:%M:%S'
    sdate = None
    cdate = datetime.now()
    try:
        sdate = cdate.strftime(format)
    except:
        raise ValueError
    return sdate

def build_data_list(inputCSV):
    sKey = []
    fn = inputCSV
    ra = csv.DictReader(file(fn), dialect="excel")
    
    for record in ra:
        #print record[ra.fieldnames[0]], type(record[ra.fieldnames[-1]])
        for item in ra.fieldnames:
            temp = float(record[item])
            sKey.append(temp)
    sKey = np.array(sKey)
    sKey.shape=(-1,len(ra.fieldnames))
    return sKey

def find(list, target):
    k = 0
    range = 0.31
    for l in list:
        if (l > target[k] + range) or (l < target[k] - range):
            return 0
        k += 1
    return 1

#--------------------------------------------------------------------------
#MAIN

if __name__ == "__main__":
    print "begin at " + getCurTime()
    inputCSV = 'C:/Users/Hu/Desktop/data.csv'
    data = build_data_list(inputCSV)

    output = []
    i = 0
    for item in data:
        if find(item, data[-4,:]) > 0:
            print i, item
            output.append(i)
            #output.append(item)
        i += 1
    #output = np.array(output)
    #output.shape = (-1, 11)
    print output
    #np.savetxt(fileLoc, countMeasure, delimiter=',', fmt = '%10.5f')


    print "end at " + getCurTime()
    print "========================================================================"  

           
